package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.UserDao;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.util.GetSqlSession;
import com.cn.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserImpl implements UserDao {
    @Override
    public int add(UserModel userModel) {
////        1.封装sql语句
//        String sql = "insert into user(username,password,phone,sex,birthday,hobby,introduce) " +
//                "values('" + userModel.getUsername() + "'," +
//                "'" + userModel.getPassword() + "'," +
//                "'" + userModel.getPhone() + "'," +
//                "'" + userModel.getSex() + "'," +
//                "'" + userModel.getBirthday() + "'," +
//                "'" + userModel.getHobby() + "'," +
//                "'" + userModel.getIntroduce() + "')";
////        2.调用数据库的执行方法
//        return JDBC.excuteUpdate(sql);
        return GetSqlSession.getSqlSession().insert("com.cn.wanxi.dao.UserDao.add", userModel);
    }

    @Override
    public boolean findByUsername(String username) {
        return GetSqlSession.getSqlSession().selectOne("com.cn.wanxi.dao.UserDao.findByUsername", username) != null;
////        1.封装sql语句
//        String sql = "select * from user where username='" + username + "'";
////        2调用JDBC查询
//        ResultSet resultSet = JDBC.getResultSet(sql);
////        3.封装查询后的结果
//        boolean isHave = false;
//        UserModel aa=null;
//        try {
//            while (resultSet.next()) {
//               aa=new UserModel();
//        isHave=true;
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return aa==null;
////        4.返回结果集
//        return isHave;
    }

    @Override
    public int findByUsernameAndPassword(UserModel userModel) {
//        1.封装sql语句
//        2调用JDBC查询
//        3.封装查询后的结果
//        4.返回结果集
        return GetSqlSession.getSqlSession().selectOne("com.cn.wanxi.dao.UserDao.findByUsernameAndPassword", userModel);
//        String sql = "select * from user where username='" + userModel.getUsername() + "' " +
//                "and password='" + userModel.getPassword() + "'";
//        ResultSet resultSet = JDBC.getResultSet(sql);
//        int resutl = 0;
//        try {
//            while (resultSet.next()) {
//                resutl = 1;
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return resutl;
    }

    @Override
    public List<UserModel> findAll(UserModel userModel) {

        return GetSqlSession.getSqlSession().selectList("com.cn.wanxi.dao.UserDao.findAll",userModel);
    }

    @Override
    public int del(int id) {
//        String sql = "delete from user where id=" + id;
//        return JDBC.excuteUpdate(sql);
        return GetSqlSession.getSqlSession().delete("com.cn.wanxi.dao.UserDao.del", id);
    }

    @Override
    public UserModel findById(int id) {
//        String sql = "select * from user where id=" + id;
//        ResultSet resultSet = JDBC.getResultSet(sql);
//        UserModel userModel = new UserModel();
//        try {
//            while (resultSet.next()) {
//                userModel = setModel(resultSet);
//            }
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return userModel;
        return GetSqlSession.getSqlSession().selectOne("com.cn.wanxi.dao.UserDao.findById", id);
    }

    @Override
    public int update(UserModel userModel) {
//        String sql = "update user set ";
//        if (!"".equals(userModel.getUsername())) {
//            sql += "username='" + userModel.getUsername() + "',";
//        }
//        if (!"".equals(userModel.getPassword())) {
//            sql += "password='" + userModel.getPassword() + "',";
//        }
//        if (!"".equals(userModel.getSex())) {
//            sql += "sex='" + userModel.getSex() + "',";
//        }
//        if (!"".equals(userModel.getIntroduce())) {
//            sql += "introduce='" + userModel.getIntroduce() + "'";
//        }
//sql=sql.substring(0,sql.length()-1);
//        sql += " where id=" + userModel.getId();
//        return JDBC.excuteUpdate(sql);
        return GetSqlSession.getSqlSession().update("com.cn.wanxi.dao.UserDao.update", userModel);
    }


    private UserModel setModel(ResultSet resultSet) {
        UserModel userModel = new UserModel();
        try {
            userModel.setId(resultSet.getInt("id"));
            userModel.setUsername(resultSet.getString("username"));
            userModel.setPhone(resultSet.getString("phone"));
            userModel.setSex(resultSet.getString("sex"));
            userModel.setBirthday(resultSet.getString("birthday"));
            userModel.setHobby(resultSet.getString("hobby"));
            userModel.setIntroduce(resultSet.getString("introduce"));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userModel;
    }
}
